package com.yun.zhong.fu.sharecenter.sentinel;

import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @Author RenPu
 * @Date 2021/1/4 9:34
 * @Version 1.0
 * @Description: 让sentinel支持restful方式的访问方式
 **/

@Component
@Slf4j
public class MyUrlCleaner implements UrlCleaner {
    @Override
    public String clean(String s) {

        // 让 /shares/1 与 /shares/2 的返回值相同
        // 返回/shares/{number}
        //字符串分割
        String[] split = s.split("/");
        return Arrays.stream(split)
                .map(string -> {
                    if (NumberUtils.isNumber(string)) {
                        return "{number}";
                    } else {
                        return string;
                    }
                })
                .reduce((a, b) -> a + "/" + b)
                .orElse("");
    }
}
